package logic

import (
	"context"
	"goods_srv/models/mysqlinit"
	"time"

	"goods_srv/goods"
	"goods_srv/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type ListHouseLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewListHouseLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ListHouseLogic {
	return &ListHouseLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ListHouseLogic) ListHouse(in *goods.ListHouseRequest) (*goods.ListHouseResponse, error) {
	// todo: add your logic here and delete this line
	var house []mysqlinit.House
	mysqlinit.DB.Debug().Order("created_at desc").Where("status = ?", in.Status).Where("house_rent BETWEEN ? AND ?", in.StartRent, in.EndRent).Where("user_id = ?", in.UserId).Find(&house)

	var list []*goods.ListHouse
	for _, i := range house {
		listHouse := goods.ListHouse{
			Id:         int64(i.ID),
			HouseTitle: i.HouseTitle,
			HouseRent:  float32(i.HouseRent),
			Address:    i.Address,
			Community:  i.Community,
			FloorCode:  i.FloorCode,
			FloorNum:   int64(i.FloorNum),
			HouseArea:  int64(i.HouseArea),
			Status:     i.Status,
			CreatedAt:  i.CreatedAt.Format(time.DateTime),
		}
		list = append(list, &listHouse)
	}
	return &goods.ListHouseResponse{
		Lists: list,
	}, nil
}
